<?php
/**
 * Created by PhpStorm.
 * User: lyq
 * Date: 2018/8/18
 * Time: 上午1:08
 */
namespace app\bussiness\controller;

use think\Exception;
use think\Db;
use think\Session;

class Pwd extends Common
{
    // 修改密码
    public function editPwd()
    {

        if (request()->isGet())
        {
            $this->layoutModal();

            return $this->fetch();
        }

        if (request()->isAjax())
        {   $oldpassword = trim(input('oldpassword'));
            $newpassword = trim(input('newpassword'));
            $conpassword = trim(input('conpassword'));
            if(empty($oldpassword)){
                return $this->ajaxError('请输入原登录密码');
            }
            if(empty($newpassword)){
                return $this->ajaxError('请输入新登录密码');
            }
            if($newpassword!=$conpassword){
                return $this->ajaxError('新登录密码和确认登录密码不一致');
            }

            $admin_user = Db::name('bussiness')->where('id',$this->currentBussinessId())->find();

            $md5pwd = md5($admin_user['salt'].$oldpassword.$admin_user['salt']);
            if($md5pwd!=$admin_user['password']){
                return $this->ajaxError('原密码输入错误');
            }
            $password = md5($admin_user['salt'].$newpassword.$admin_user['salt']);
            $data = [
                'password'=>$password
            ];
            $save = Db::name('bussiness')
                ->where('id',$this->currentBussinessId())
                ->update($data);
            if($save){
                $this->ajaxSuccess('密码修改成功，下次登录请使用新密码！');
            }else{
                $this->ajaxError('服务器繁忙，请稍后再试');
            }

        }
    }

}